package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Menu;
import com.example.entity.Role;
import com.example.entity.RoleMenu;
import com.example.mapper.RoleMapper;
import com.example.mapper.RoleMenuMapper;
import com.example.service.MenuService;
import com.example.service.RoleMenuService;
import com.example.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hotta
 * @since 2023-05-31
 */
@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private MenuService menuService;

    @Override
    @Transactional
    public void addRole(Role role) {
        //写入角色表
        log.info("{}", role);
        this.baseMapper.insert(role);
        //写入 角色权限 关系表
        if (role.getMenuIdList() != null) {
            for (Long menuId : role.getMenuIdList()) {
                roleMenuMapper.insert(new RoleMenu(null, role.getId(), menuId));
            }
        }
    }

    @Override
    public Role getRoleById(Long id) {

        Role role = this.baseMapper.selectById(id);

        log.info("{}", role);

        Page<RoleMenu> roleMenuPage = new Page<>();

        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.select(RoleMenu::getMenuId);
        roleMenuLambdaQueryWrapper.eq(RoleMenu::getRoleId, id);
        List<RoleMenu> roleMenuList = roleMenuService.list(roleMenuLambdaQueryWrapper);

        List<Long> menuId = roleMenuList.stream().map(item -> {
            Long ids = item.getMenuId();
            return ids;
        }).collect(Collectors.toList());

        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        menuLambdaQueryWrapper.select(Menu::getId);
        menuLambdaQueryWrapper.in(Menu::getId,menuId);
        menuLambdaQueryWrapper.ne(Menu::getParentId,0);
        List<Menu> menu = menuService.list(menuLambdaQueryWrapper);

        List<Long> menuIdList = menu.stream().map(item -> {
            Long ids = item.getId();
            return ids;
        }).collect(Collectors.toList());

        role.setMenuIdList(menuIdList);

        log.info("{}", role);
        System.out.println(role.getMenuIdList().getClass());
        log.info(role.getMenuIdList().toString());

        //menu_id :2,3,1
//        List<Integer> menuIdList = role.getMenuIdList();
//        menuIdList.remove(1);
        return role;
    }

    @Override
    @Transactional
    public void updateRole(Role role) {
        //修改角色表，删除原有权限，新增权限
        this.baseMapper.updateById(role);

        LambdaQueryWrapper<RoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoleMenu::getRoleId, role.getId());
        roleMenuMapper.delete(wrapper);

        if (role.getMenuIdList() != null) {
            for (Long menuId : role.getMenuIdList()) {
                roleMenuMapper.insert(new RoleMenu(null, role.getId(), menuId));
            }
        }
    }

    @Override
    public void deleteRoleById(Long id) {

        this.baseMapper.deleteById(id);

        LambdaQueryWrapper<RoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoleMenu::getRoleId, id);
        roleMenuMapper.delete(wrapper);

    }
}
